#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# -- 文件转换
# --
# ****************************
# Author: lmay.Zhou
# Blog: www.lmaye.com
# Date: 2021/3/4 14:03
# Email lmay@lmaye.com
# ----------------------------------------------------------
import os
from pathlib import Path

from constant.sys_enum import SysEnum
from handler.thread_handler import thread_list

# def handler_thread(_file_names, eml_path_, csv_path, lens=50000):
#     threads_ = []
#     for i in range(math.ceil(len(_file_names) / lens)):
#         records = _file_names[i * lens:(i + 1) * lens]
#         thread = threading.Thread(target=write_file, args=(i, records, eml_path_, csv_path))
#         threads_.append(thread)
#     return threads_
#
#
# def write_file(x, _file_names, eml_path_, csv_path):
#     LOG.info("线程[{}]执行中...[{}] - [{}]".format(x + 1, threading.currentThread().name, threading.currentThread().ident))
#     records = []
#     for file_name in _file_names:
#         eml_file = eml_path_ + file_name
#         eml = eml_utils.read_eml(eml_file, content_type="text/plain")
#         if eml:
#             # jdbc_util.sql_execute(sql, "onlyOne", connect=connect, args=eml)
#             records.append(eml)
#             LOG.info("线程[{}]处理了[{}]".format(threading.currentThread().name, len(records)))
#     header = ["subject", "from", "to", "content"]
#     excel_utils.write_csv(csv_path + str(round(time.time() * 1000)) + ".csv", header, records)
#     # jdbc_util.sql_execute(sql, "insertAll", connect=connect, args=records)


if __name__ == "__main__":
    # db_yml = read_yml(SysEnum.DB_PATH.value)
    # connect = jdbc_util.sql_connect(db_yml["db"])
    # sql = "INSERT INTO `eml`.`tb_eml_2`(`subject`, `from`, `to`, `content`) VALUES (%s, %s, %s, %s)"

    # path = SysEnum.RESOURCES_PATH.value + SysEnum.SEPARATOR.value
    # eml_path = path + "eml" + SysEnum.SEPARATOR.value
    # file_names = os.listdir(eml_path)
    # # 1. 单线程
    # records = []
    # header = ["subject", "from", "to", "content"]
    # csv_path = path + "csv" + SysEnum.SEPARATOR.value
    # for file_name in file_names:
    #     eml = eml_utils.read_eml(eml_path + file_name)
    #     if eml:
    #         records.append(eml)
    #     if 100 == len(records):
    #         # json_utils.write_json(csv_path + str(round(time.time() * 1000)) + ".json", records)
    #         # excel_utils.write_excel(csv_path + str(round(time.time() * 1000)) + ".xlsx", "Sheet1", records)
    #         excel_utils.write_csv(csv_path + str(round(time.time() * 1000)) + ".csv", header, records)
    #         # jdbc_util.sql_execute(sql, "insertAll", connect=connect, args=records)
    #         records.clear()
    # # # 剩余的处理
    # # jdbc_util.sql_execute(sql, "insertAll", connect=connect, args=records)
    # # json_utils.write_json(csv_path + str(round(time.time() * 1000)) + ".json", records)
    # # excel_utils.write_excel(csv_path + str(round(time.time() * 1000)) + ".xlsx", "Sheet1", records)
    # excel_utils.write_csv(csv_path + str(round(time.time() * 1000)) + ".csv", header, records)

    # 2. 多线程
    # EML目录
    eml_path = "F:\\tmp\\客服邮件"
    # 输出目录
    out_path = "F:\\csv"
    # 邮件内容类型
    content_type = "text/plain"
    # 处理数量
    lens = 10000
    threads = []
    for root, dirs, files in os.walk(eml_path):
        if 0 < len(files):
            file_names = os.listdir(root)
            csv_path = Path(out_path + root.replace(eml_path, ""))
            if not csv_path.exists():
                os.makedirs(csv_path)
            thread_list(threads, file_names, root + SysEnum.SEPARATOR.value, str(csv_path) + SysEnum.SEPARATOR.value,
                        lens, content_type)
    for t in threads:
        t.start()
    for t in threads:
        t.join()
